package com.xiaoran.robot.config;
import com.alibaba.fastjson.JSONObject;
import com.xiaoran.robot.utils.MessageParser;
import com.xiaoran.robot.utils.StringUtils;
import org.java_websocket.client.WebSocketClient;
import org.java_websocket.handshake.ServerHandshake;

import java.net.URI;

public class MyWebsocketClient extends WebSocketClient {
    public MyWebsocketClient(URI serverUri) {
        super(serverUri);
    }
    @Override
    public void onOpen(ServerHandshake serverHandshake) {
        System.out.println("消息监听开始");
    }

    @Override
    public void onMessage(String s) {
        JSONObject jsonData = StringUtils.toJson(s);
        MessageParser instance = MessageParser.getInstance();
        instance.setMsg(jsonData);
        instance.parseMessage();
    }

    @Override
    public void onClose(int i, String s, boolean b) {
        System.out.println(s);
        System.out.println("消息监听结束");
    }

    @Override
    public void onError(Exception e) {
        System.out.println(e.toString());
        System.out.println("错误");
    }
}
